package com.e_manager.soft.emanager.controller;

import com.e_manager.soft.emanager.annotation.Authorization;
import com.e_manager.soft.emanager.annotation.BaseController;
import com.e_manager.soft.emanager.annotation.CurrentUser;
import com.e_manager.soft.emanager.entity.InvitationRecord;
import com.e_manager.soft.emanager.entity.MstMessage;
import com.e_manager.soft.emanager.entity.User;
import com.e_manager.soft.emanager.entity.parameter.MstMessageParameter;
import com.e_manager.soft.emanager.rabbitMQ.AgreementRabbitMQ;
import com.e_manager.soft.emanager.resultMsg.ResultConstant;
import com.e_manager.soft.emanager.resultMsg.ResultInfo;
import com.e_manager.soft.emanager.service.InvitationRecordService;
import com.e_manager.soft.emanager.service.MstMessageService;
import com.e_manager.soft.emanager.token.TokenConstant;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * @author 徐玲玉
 * 2018/3/13 0013
 */
@BaseController
@Authorization
@Api(value = "/message", description = "报文管理表")
public class MstMessageController {
    @Autowired
    private MstMessageService msMessageService;

    @Autowired
    private InvitationRecordService invitationRecordService;

    @PutMapping(value = "/onMessage", produces = {"application/json;charset=utf-8"})
    public void onMessage() {
        Message msg = null;
        AgreementRabbitMQ message = new AgreementRabbitMQ();
        message.onMessage(msg);
    }

    @PostMapping(value = "/findMessage", produces = {"application/json;charset=utf-8"})
    public ResultInfo<PageInfo> findMessage(@RequestBody MstMessageParameter mp, @CurrentUser User iuser) {
        ResultInfo<PageInfo> resultInfo = new ResultInfo<>();
        try {
            if (!"".equals(mp.getMessageFlag()) && mp.getMessageFlag() != null) {
                InvitationRecord record = new InvitationRecord();
                record.setUserID(iuser.getUserID());
                record.setPageSize(mp.getPageSize());
                record.setPageNumber(mp.getPageNumber());
                PageInfo<InvitationRecord> info = invitationRecordService.findAllByPage(record);
                resultInfo.setData(info);
            } else {
                mp.setUserId(iuser.getUserID());
                mp.setMessageType(TokenConstant.ONE);
                PageInfo<MstMessage> info = msMessageService.findMessageByPage(mp);
                resultInfo.setData(info);
            }

            resultInfo.setMsg(ResultConstant.SUCCESS.getMessage());
            resultInfo.setCode(ResultConstant.SUCCESS.getCode());
        } catch (Exception e) {
            resultInfo.setMsg(ResultConstant.GET_MESSAGE_FIIL.getMessage());
            resultInfo.setCode(ResultConstant.GET_MESSAGE_FIIL.getCode());
            e.printStackTrace();
        }
        return resultInfo;
    }
}
